﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>List Unpaid Bill</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script>

	jQuery(document).ready(function(){
		jQuery("#patientDialog").dialog({
			autoOpen:false,
			width:600,
			modal:true,
			buttons:[
			         {
				         text:"send Selected",
				         click:function(){
					         
					         var mobileSelectList = document.getElementsByName("mobileSelect");
					         var telephone = "";
					         var index = 0;
					         for( var i = 0; i < mobileSelectList.length; i++ ){
						         if( mobileSelectList[i].checked == true ){
						        	 
							         if( index == 0 ){
								     	telephone += mobileSelectList[i].value;   
							         }else{
							        	 telephone += ",";
							        	 telephone += mobileSelectList[i].value;   
							         }
							         index++;
						         }
					         }

					         window.location.href = "sms_message.php?telephone=" + telephone;
				         }
			         }
					]
			});

		var datepicker_CurrentInput;  
        jQuery.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        jQuery(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		
		jQuery("#startDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
		jQuery("#endDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});

	function listPatientNo(){
		
		
		jQuery("#patientDialog").dialog("open");
	}

	function FormSubmit(){
		var startDateObject = document.getElementById("startDate");
		var endDateObject = document.getElementById("endDate");
		var startDate = startDateObject.value;
		var endDate = endDateObject.value;


		if( startDate == "" && endDate == "" ){
			document.getElementById("errorMsg").innerHTML = "please input date!!!";
			startDateObject.focus();
			return;
		}
		
		var startMill = 0;
		var endMill = 0;

		if( startDate != "" ){
			var startDateArray = startDate.split("/");
			startDate = startDateArray[1] + "/" + startDateArray[0] + "/" + startDateArray[2];
			var startDateObject = new Date(startDate);
			startMill = startDateObject.getTime();
		}

		if( endDate == "" ){
			var endDateObject = new Date();
			endMill = endDateObject.getTime();
		}else{
			var endDateArray = endDate.split("/");
			endDate = endDateArray[1] + "/" + endDateArray[0] + "/" + endDateArray[2];
			var endDateObject = new Date(endDate);
			endMill = endDateObject.getTime();
		}

		if( startMill > endMill ){
			document.getElementById("errorMsg").innerHTML = "end should be after the start!";
			startDateObject.focus();
			return;
		}

		document.getElementById("listUnpaid").submit();
		
	}
	


	function setIsPay(appointmentNo, isPay, isPayId){
		var isPayStatus;
		if( isPay == true ){
			isPayStatus = 1;
		}else{
			isPayStatus = 0;
		}
		jQuery.ajax({type:'post',async:'false', url:'set_pay.php', data:"appointmentNo=" + appointmentNo + "&isPayStatus=" + isPayStatus, dataType:'json', success:function(result){
				if( result.data.status == "success" ){
					
					alert(result.data.msg);
				}else{
					if( isPay == true ){
						document.getElementById(isPayId).checked = false;
					}else{
						document.getElementById(isPayId).checked = true;
					}
					alert(result.data.msg);
					
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				if( isPay == true ){
					document.getElementById(isPayId).checked = false;
				}else{
					document.getElementById(isPayId).checked = true;
				}
				alert(XMLHttpRequest.responseText);
			}
				
		});
		
		
	}
</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
		
		$ini_array = parse_ini_file("config/config.ini");
			
		date_default_timezone_set($ini_array["default_timezone"]);
			
		$resultNumber = 0;
		$startDate = "";
		$endDate = "";
		$reportResult;
			
		$paymentMethod = "";
		if( isset($_POST["paymentMethod"])){
			$paymentMethod = $_POST["paymentMethod"];
		}
			
		if( isset($_POST["startDate"]) || isset($_POST["endDate"]) ){
			if( isset($_POST["startDate"]) && strcmp($_POST["startDate"],"") != 0){
				$startDate = $_POST["startDate"];
			}else{
				$startDate = "01/01/1970";
			}
		
			if( isset($_POST["endDate"]) &&  strcmp($_POST["endDate"],"") != 0 ){
				$endDate = $_POST["endDate"];
			}else{
				$endDate = date("d/m/Y");
		
			}
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$startDate = mysqli_real_escape_string($conn,$startDate);
			$endDate = mysqli_real_escape_string($conn, $endDate);
		
		
		
		
			$getinfoSql = "SELECT visit.`Appointment_no`, visit.`Datetime_patient_seen`, visit.`Professional_fee`, 
			visit.`Bill_priority`,visit.`payment_method`, patient.`Patient_no`, patient.`First_name`, patient.`Surname`, patient.`address`, patient.`mobile`, appointment.`isFirstVisit`  
			FROM visit, patient, appointment WHERE visit.`isPay`=FALSE AND visit.`Patient_no`=patient.`Patient_no` AND appointment.`appointment_no`=visit.`Appointment_no` and  datetime_patient_seen > STR_TO_DATE('".$startDate."','%d/%m/%Y') AND datetime_patient_seen < STR_TO_DATE('".$endDate."','%d/%m/%Y')";
		
			if( strcmp($paymentMethod,"") != 0 ){
				$getinfoSql .= "  and payment_method='".$paymentMethod."'";
			}
			
			$getinfoSql .= "  order by Datetime_patient_seen desc";
			$infoResult = mysqli_query($conn, $getinfoSql);
			$resultNumber = mysqli_num_rows($infoResult);
		
		}else{
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$getinfoSql = "SELECT visit.`Appointment_no`, visit.`Datetime_patient_seen`, visit.`Professional_fee`, 
			visit.`Bill_priority`,visit.`payment_method`, patient.`Patient_no`, patient.`First_name`, patient.`Surname`, patient.`address`, patient.`mobile`, appointment.`isFirstVisit`  
			FROM visit, patient, appointment WHERE visit.`isPay`=FALSE AND visit.`Patient_no`=patient.`Patient_no` AND appointment.`appointment_no`=visit.`Appointment_no` ";
		
			if( strcmp($paymentMethod,"") != 0  ){
				$getinfoSql .= "  and  payment_method='".$paymentMethod."'";
			}
			$getinfoSql .= "  order by Datetime_patient_seen desc";
			$infoResult = mysqli_query($conn, $getinfoSql);
			$resultNumber = mysqli_num_rows($infoResult);
		
		}
			
			
			
		?>
		<div id="search">
			<form  name="form1" method="post" action="list_unpaid.php" id="listUnpaid">
				<label>Start Date:</label><input type="text" name="startDate" id="startDate" value="<?php echo $startDate?>"  readonly="readonly"/>
				<label>End Date:</label><input type="text" name="endDate" id="endDate" value="<?php echo $endDate?>" readonly="readonly"/>
				<label>Payment Method:</label>
				<select name="paymentMethod" id="paymentMethod" onchange="changeFunc()">
					<option value=""></option>
					<option value="VHI" <?php if( strcasecmp("VHI", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>VHI</option>
					<option value="Aviva" <?php if( strcasecmp("Aviva", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Aviva</option>
					<option value="Cash" <?php if( strcasecmp("Cash", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cash</option>
					<option value="Cheque" <?php if( strcasecmp("Cheque", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cheque</option>
					<option value="Quinn" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Quinn</option>
					<option value="Bo Fee" <?php if( strcasecmp("Bo Fee", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Bo Fee</option>
					<option value="Bupa" <?php if( strcasecmp("Bupa", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Bupa</option>
					<option value="ESB" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>ESB</option>
					<option value="Garda" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Garda</option>
					<option value="No Fee" <?php if( strcasecmp("No Fee", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>No Fee</option>
					<option value="Paid" <?php if( strcasecmp("Paid", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Paid</option>
					<option value="Unpaid" <?php if( strcasecmp("Unpaid", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Unpaid</option>
					<option value="Other" <?php if( strcasecmp("Other", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Other</option>	
				</select>
				<input type="button" name="listUnpaid" value="list" onclick="FormSubmit()" />
				<label id="errorMsg"></label>
			</form>
			
		</div>
		<div id="main_content" style="text-align:left;">
			<div id="title" style="text-align:center;"><h2>Unpaid Bill List</h2></div>
			<div style="display:none;">
				<div style="width:105px;display:inline-block;text-align:left;position:relative;left:15px;"><label>No.</label></div>
				<div style="width:100px;display:inline-block"><label>Date Seen</label></div>
				<div style="width:146px;display:inline-block"><label>Patient Name</label></div>
				<div style="width:155px;display:inline-block"><label>Address</label></div>
				<div style="width:180px;display:inline-block"><label>TelNo.</label></div>
				<div style="width:80px;display:inline-block"><label>Fee</label></div>
				<div style="width:80px;display:inline-block"><label>Method</label></div>
				<div style="width:105px;display:inline-block"><label>First Visit</label></div>
				<div style="width:120px;display:none"><label>Bill Priority</label></div>
				<div style="width:80px;display:inline-block"><label>IsPay</label></div>
				<div style="width:80px;display:inline-block"><label>Demand</label></div>
			</div>
			<div id="unpaidList" style="margin:0px auto;width:1180px;max-height:500px;overflow:auto;">
				<table style="width:1160px;">
					<thead>
						<tr>
							<th><label>No.</label></th>
							<th><label>Date Seen</label></th>
							<th><label>Patient Name</label></th>
							<th><label>Address</label></th>
							<th><label>TelNo.</label></th>
							<th><label>Fee</label></th>
							<th><label>Method</label></th>
							<th><label>FirstVisit</label></th>
							<th><label>IsPay</label></th>
							<th><label>Demand</label></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td style="height:0.5em"></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
				<?php 
				$totalUnpaid = 0;
				if( $resultNumber != 0 ){
					while( $row = mysqli_fetch_array($infoResult)){
						$appointmentNo = $row["Appointment_no"];
						$datetimeSeen = date("d/m/Y",strtotime($row["Datetime_patient_seen"]));
						$fee = $row["Professional_fee"];
						$totalUnpaid += $fee;
						$isFirstVisit = $row["isFirstVisit"];
						$billPriority = $row["Bill_priority"];
						$paymentMethod = $row["payment_method"];
						$patientNo = $row["Patient_no"];
						$patientFirstName = $row["First_name"];
						$patientSurname = $row["Surname"];
						$patientAddress = $row["address"];
						if( strlen($patientAddress) > 35 ){
							$address = substr($patientAddress, 0, 35);
						}
						$patientMobile = $row["mobile"];
						//echo "1".$patientMobile."1";
				?>
					<tr>
						<td style="width:10%;"><?php echo $patientNo?></td>
						<td style="width:10%;"><?php echo $datetimeSeen?></td>
						<td style="width:15%;"><?php echo $patientFirstName." ".$patientSurname?></td>
						<td style="width:10%;"><?php echo $patientAddress?></td>
						<td style="width:10%;"><?php echo $patientMobile?></td>
						<td style="width:8%;">€<?php echo $fee?></td>
						<td style="width:12%;"><?php echo $paymentMethod?></td>
						<td style="width:10%;"><?php if( $isFirstVisit == 1 ){echo "yes";}else{echo "no";}?></td>
						<td style="width:10%;"><input type="checkbox" id="isPay<?php echo appointmentNo?>" onclick="setIsPay('<?php echo $appointmentNo?>', this.checked, this.id)"/></td>
						<td style="width:10%;"><a href="fee_demand_pdf.php?appointmentNo=<?php echo $appointmentNo?>">generate</a></td>
					</tr>
				
				<?php 
					}
				}
?>
				<tbody>
				</table>
			</div>
			<div style="text-align:center;margin:10px 0px;"><label>Total Unpaid:€</label><label><?php echo round($totalUnpaid, 2)?></label></div>
			<div style="text-align:center;margin:10px 0px;">
				<input type="button" value="Send Message To Selected Patient" onclick="listPatientNo()"/>
				<input type="button" value="return" onclick="window.location.href='index.php'"/>
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
	<?php 
		$getPatientSql = "SELECT patient.`Patient_no`, patient.`First_name`, patient.`Surname`, total_fee, patient.mobile 
		FROM (SELECT  Patient_no, SUM(visit.`Professional_fee`) total_fee FROM visit WHERE ispay=FALSE GROUP BY Patient_no) 
		AS visitTable, patient WHERE patient.`Patient_no`=visitTable.Patient_no";
		$result = mysqli_query($conn, $getPatientSql);
		
		
	?>
	
	<div id="patientDialog" title="List Unpaid Patient">
		<div style="display:inline-block;width:100px;"><label>PatientNo</label></div>
		<div style="display:inline-block;width:100px;"><label>First Name</label></div>
		<div style="display:inline-block;width:100px;"><label>Surname</label></div>
		<div style="display:inline-block;width:100px;"><label>Total Fee</label></div>
		<div style="display:inline-block;width:100px;"><label>isSelected</label></div>
		
		
		
		
		<div id="dataDiv" style="width:550px;max-height:200px;overflow:auto;">
			<table style="width:540px"> 
				<?php 
					while( $row = mysqli_fetch_array($result)){
						$patientNo = $row["Patient_no"];
						$firstName = $row["First_name"];
						$surname = $row["Surname"];
						$totalFee = $row["total_fee"];
						$mobile = $row["mobile"];
					
						
				?>
					<tr>
						<td style="width:20%;"><?php echo $patientNo?></td>
						<td style="width:20%;"><?php echo $firstName?></td>
						<td style="width:20%;"><?php echo $surname?></td>
						<td style="width:20%;"><?php echo $totalFee?></td>
						<td style="width:20%;"><input type="checkbox" value="<?php echo $mobile?>" name="mobileSelect" totalFee="<?php echo $totalFee?>" patientNo="<?php echo $patientNo?>"/></td>
					</tr>
				<?php }?>
			</table>
		</div>
	</div>
	
</body>
</html>
